package org.zhongyin.coolingload;

import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import jxl.read.biff.BiffException;

import org.zhongyin.Dao.ARCIVE_DBDaoImpl;
import org.zhongyin.JPA.Entity.PermanentHistoryData;


public class NoaaDataWeekDays implements INoaaDataWeek{
	private IData noaaData;
	
	public NoaaDataWeekDays() throws ParseException{
		try {
			this.noaaData = new NoaaData();
//			noaaData.setInputFile("c:/CoolingLoadProject/Original Data 20100101-20130101.xls");
			ARCIVE_DBDaoImpl dao = new ARCIVE_DBDaoImpl();
			
			List<Object[]> rData =dao.getRecentData();
			List<Object[]> today =dao.getTodayData();
			List<PermanentHistoryData> clData = dao.getHistCLData();
			
			noaaData.read(dao.combineData(today, rData, clData));
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @return return the nooaData from Database or Excel
	 */
	public IData getNoaaData(){
		return this.noaaData;
	}
	
	/**
	 * 
	 * @param localTime the local time in database
	 * @param n   n the parameter in ARX(1,2,24) || ARX(24,168);
	 * @return
	 * @throws BiffException
	 * @throws IOException
	 * @throws ParseException
	 */
	
	public IData getLastNHour(Date localTime, int n) throws BiffException, IOException, ParseException{
		
		IData lastHourData = new NoaaData();
		
		HashMap<Date, Float> T_t = noaaData.getT_t();
		HashMap<Date, Integer> dayOfWeek = noaaData.getDayofWeek();
		HashMap<Date, Integer> hourOfDay = noaaData.getHour();
		HashMap<Date, Float> T_MAX_t = noaaData.getT_MAX_t();
		HashMap<Date, Float> T_MIN_t = noaaData.getT_MIN_t();
		HashMap<Date, Float> RH_t = noaaData.getRH_t();
		HashMap<Date, Float> CoolLoad_t = noaaData.getCoolLoad_t();
		HashMap<Date, String> time = noaaData.getLocalTime();
		
		
		
		
		Date date = (Date) localTime.clone();
		int currentDay = dayOfWeek.get(date);
//		Utility.debug(currentDay);

		
		
		//Monday to Friday 
		if(currentDay>= 1 && currentDay<=5){
			date.setHours(date.getHours()-n);
			
			if(date.getDay() == 0){
				date.setHours(date.getHours()-48);
			}
//			System.out.println(date);
			//last hour T_Max_t
			HashMap<Date, Float> lastHour_T_MAX_t = new HashMap<Date, Float>();
			lastHour_T_MAX_t.put(date, T_MAX_t.get(date));	
			lastHourData.setT_MAX_t(lastHour_T_MAX_t);
			lastHourData.setTmaxt(T_MAX_t.get(date));
			
			//last hour T_Min_t
			HashMap<Date, Float> lastHour_T_MIN_t = new HashMap<Date, Float>();
			lastHour_T_MIN_t.put(date, T_MIN_t.get(date));	
			lastHourData.setT_MIN_t(lastHour_T_MIN_t);
			lastHourData.setTmint(T_MIN_t.get(date));
			
			//last hour RH_t
			HashMap<Date, Float> lastHour_RH_t = new HashMap<Date, Float>();
			lastHour_RH_t.put(date, RH_t.get(date));	
			lastHourData.setRH_t(lastHour_RH_t);
			lastHourData.setRHt(RH_t.get(date));
			
			//last hour T_t
			HashMap<Date, Float> lastHour_T_t = new HashMap<Date, Float>();
			lastHour_T_t.put(date, T_t.get(date));
			lastHourData.setT_t(lastHour_T_t);
			lastHourData.setTt(T_t.get(date));
			
			//last hour Cooling Load
			HashMap<Date, Float> lastHour_CoolLoad_t = new HashMap<Date, Float>();
			lastHour_CoolLoad_t.put(date, CoolLoad_t.get(date));
			lastHourData.setCoolLoad_t(lastHour_CoolLoad_t);
			lastHourData.setCoolLoadt(CoolLoad_t.get(date));
			
			//last hour day of week
			HashMap<Date, Integer> lastHour_dayOfWeek = new HashMap<Date, Integer>();
			lastHour_dayOfWeek.put(date, dayOfWeek.get(date));
			lastHourData.setDayofWeek(lastHour_dayOfWeek);
			
			//last hour 
			HashMap<Date, Integer> lastHour_hourOfDay = new HashMap<Date, Integer>();
			lastHour_hourOfDay.put(date, hourOfDay.get(date));
			lastHourData.setHour(lastHour_hourOfDay);
			
			//last hour local time
			HashMap<Date, String> lastHour_localTime = new HashMap<Date, String>();
			lastHour_localTime.put(date, time.get(date));			
			lastHourData.setLocalTime(lastHour_localTime);
		}
		else{
			System.out.println("The day you input is not bewteen Monday and Friday");
		}
		
		return lastHourData;
	}
	
	

	
	public void toString(IData data){
		System.out.println("Local Time"+data.getLocalTime().toString());
		System.out.println("Day of the week"+data.getDayofWeek().toString());
		System.out.println("hour"+ data.getHour().toString());
		System.out.println("Max t"+ data.getT_MAX_t().toString());
		System.out.println("Min t"+ data.getT_MIN_t().toString());
		System.out.println("T_t" + data.getT_t().toString());
		System.out.println("RH_t"+ data.getRH_t().toString());
		System.out.println("Cool Load t"+data.getCoolLoad_t().toString());
		
		System.out.println("Max t"+ data.getTmaxt());
		System.out.println("Min t"+ data.getTmint());
		System.out.println("T_t" + data.getTt());
		System.out.println("RH_t"+ data.getRHt());
		System.out.println("Cool Load t"+data.getCoolLoadt());
		
	}
	
	public static void main(String args[]) throws BiffException, IOException, ParseException{
//		NoaaDataWeekDays data = new NoaaDataWeekDays();
//		Date date = Utility.convertToDate("7/18/13 1:00");
//		IData last_hour_data= data.getLastNHour(date, 2);

//		data.toString(last_hour_data);
		
		ARXWeekDays test = new ARXWeekDays();
		
		test.setARXmodel();
//		
		Utility.printArraySingleLine(test.getARXmodel_X());
		System.out.println(Arrays.toString(test.getARXmodel_Y()));
		
		System.out.println(test.getARXmodel_X().length + " "  + test.getARXmodel_Y().length);

		
		

		
	}
	
	
	
}
